home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / rettig.arc / TRSOURCE.EXE / FACT.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  1KB  |  45 lines

  1. /*********
  2. *
  3. * FACT.C
  4. *
  5. * by Ralph Davis
  6. *
  7. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  8. *
  9. *  Syntax:  FACT( <expN> )
  10. *  Return:  <expn1> factorial if <expN> <= 18
  11. *             (above 18 precision is lost)
  12. *           otherwise _tr_infinity()
  13. *********/
  14.  
  15. #include "trlib.h"
  16.  
  17. TRTYPE fact()
  18. {
  19.     int i;
  20.     double ret;
  21.  
  22.     if ( PCOUNT == 1 && ISNUM(1) )
  23.     { 
  24.        i = _parni(1);
  25.        if (i > 18)
  26.           _retnd(_tr_infinity());  /* If number is greater than 18,
  27.                                       precision disappears, so
  28.                                       return infinity as error signal */
  29.  
  30.        else if (i <= 0)            /* If number is 0 or negative,
  31.                                       return 0 */
  32.           _retnd( 0.0 );
  33.        else
  34.        {
  35.           ret = 1.0;
  36.           for (; i > 1; i--)       /* This loop computes the product
  37.                                       of all the numbers from i to 1. */
  38.              ret *= (double) i;
  39.           _retnd( ret ); 
  40.         }
  41.      }
  42.      else
  43.         _retnd( ERRORNEG );        /* if error return -1 */
  44. }
  45.